<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-01-01 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);

		$this->excute();
	}

	function excute()
	{
		ob_start();

		switch($this->act)
		{
			default:
				$this->main();
				break;
		}

		$body = ob_get_clean();

		$this->showBody($body);
	}

	private function main()
	{
		$page = $this->page_num + 1;
		
		$query_link="admin.php?m=session";
		$where_list = array();

		$username = isset($this->request['username']) ? trim($this->request['username']) : '';
		if ($username != '')
		{
			$where_list[] = "user_name = '$username'";
			$query_link .= "&username=".urlencode($username);
		}
		
		if(isset($this->request['is_member']))
		{
			$is_member = (int)$this->request['is_member'];
			$where_list[] = $is_member > 0 ? "uid > 0" : "uid < 0";
		}

		$ip = isset($this->request['ip']) ? trim($this->request['ip']) : '';
		if($ip != '')
		{
			$ip_l = explode('.', $ip);
			$ips = '';
			$i = 0;
			foreach($ip_l as $_ip)
			{
				$i++;
				if($_ip == "*" || empty($_ip))continue;
				
				$ips .= $and."ip{$i}='$_ip'";
				$and = " and ";
			}
			$where_list[]=$ips;
		}

		$order = 'order by last_activity desc';
		
		$order_by = isset($this->request['order_by']) ? trim($this->request['order_by']) : '';
		if($order_by != '' && in_array($order_by, array('last_activity', 'ip', 'uid')))
		{
			$query_link .= "&order_by=$order_by";
			$order = 'order by '.$order_by;
		}
		
		$order_type = isset($this->request['order_type']) ? trim($this->request['order_type']) : '';
		if($order_type != '')
		{
			$query_link .= "&order_type=$order_type";
			$order .= ($order_type == 'asc') ? 'asc' : 'desc';
		}

		$where = (empty($where_list)) ? null : ' WHERE '.implode(' AND ',$where_list).' ';

		$order_by_list = array
		(
			'order_by_default' => 'last_activity',
			'last_activity' => array
			(
				'name' => '最后访问时间',
				'order_by' => 'last_activity'
			),
			'ip' => array
			(
				'name' => 'IP地址',
				'order_by' =>"concat_ws('.',ip1,ip2,ip3,ip4)"
			),
			'uid' => array
			(
				'name' => '用户名',
				'order_by' => 'uid'
			),
		);
		
		$sql = "SELECT count(1) total from ".TABLE_PREFIX."sessions".$where;
		$total_record = DBFunction::getInstance()->getOne($sql);

		$session_list = array();
		if($total_record > 0)
		{
			$offset = $this->page_num * $this->page_limit;
			$sql = "SELECT * from ".TABLE_PREFIX."sessions".$where." ".$order." limit {$offset}, {$this->page_limit}";
			$results = DBFunction::getInstance()->getAll($sql);
		
			foreach($results as $row)
			{
				$row['dateline'] = date('Y-m-d H:i:s', $row['last_activity']);
				$row['ip'] = sprintf("%s.%s.%s.%s", $row['ip1'], $row['ip2'], $row['ip3'], $row['ip4']);
				$session_list[] = $row;
			}
		}

		$order_html = '';
		//$pages = page($total,$pn, $query_link, array(), "2 10 20 50 100 200 500");
		$page_html = page($total_record, $this->page_limit, $page, $query_link, '20 50 100 200 500');

		$action = "admin.php?m=session";
		include template('admin/sessions');
		
	}
}